using System;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using DoveSiButta.Helpers;
using DoveSiButta.Models;
using DoveSiButta.Services;

namespace DoveSiButta.Controllers
{

    public class JsonBox
    {
        public int BoxID { get; set; }
        public DateTime EventDate { get; set; }
        public string Title { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string Description { get; set; }
        public int RatingCount { get; set; }
        public string Url { get; set; }
    }

    [HandleErrorWithELMAH]
    public class SearchController : Controller
    {

        IBoxRepository boxRepository;

        //
        // Dependency Injection enabled constructors

        public SearchController()
            : this(new BoxRepository())
        {
        }

        public SearchController(IBoxRepository repository)
        {
            boxRepository = repository;
        }

        //
        // AJAX: /Search/FindByLocation?longitude=45&latitude=-90

        [HttpPost]
        public ActionResult SearchByLocation(float latitude, float longitude)
        {
            var boxes = boxRepository.FindByLocation(latitude, longitude);

            var jsonBoxes = from dinner in boxes.AsEnumerable()
                              select JsonBoxFromBox(dinner);

            return Json(jsonBoxes.ToList());
        }

        [HttpPost]
        public ActionResult SearchByPlaceNameOrZip(string placeOrZip)
        {
            if (String.IsNullOrEmpty(placeOrZip)) return null; ;
            LatLong location = GeolocationService.PlaceOrZipToLatLong(placeOrZip);

            var boxes = boxRepository.
                            FindByLocation(location.Lat, location.Long).
                            OrderByDescending(p => p.EventDate);

            return View("Results", new PaginatedList<Box>(boxes, 0, 20));
        }

     
        //
        // AJAX: /Search/GetMostPopularBoxes
        // AJAX: /Search/GetMostPopularBoxes?limit=5

        [HttpPost]
        public ActionResult GetMostPopularBoxes(int? limit)
        {
            var boxes = boxRepository.FindUpcomingEvents();

            // Default the limit to 40, if not supplied.
            if (!limit.HasValue)
                limit = 40; 

            //var mostPopularBoxes = from dinner in boxes
            //                         orderby dinner.Ratings.Count descending
            //                         select dinner;

            var mostPopularBoxes = from dinner in boxes //GM 2012-05-14 //Ticket #18
                                   orderby dinner.EventDate descending
                                   select dinner;


            var jsonBoxes =
                mostPopularBoxes.Take(limit.Value).AsEnumerable()
                .Select(item => JsonBoxFromBox(item));

            return Json(jsonBoxes.ToList());
        }

        private JsonBox JsonBoxFromBox(Box box)
        {
            return new JsonBox
            {
                BoxID = box.BoxID,
                EventDate = box.EventDate,
                Latitude = box.Latitude,
                Longitude = box.Longitude,
                Title = box.Title,
                Description = box.Description,
                RatingCount = box.Ratings.Count,

                //TODO: Need to mock this out for testing...
                //Url = Url.RouteUrl("PrettyDetails", new { Id = box.BoxID } )
                Url = box.BoxID.ToString()
            };
        }

    }
}